package com.byght.bygsystem.controller;

import com.byght.bygsystem.model.Transaction;
import com.byght.bygsystem.service.TransactionService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api/transactions")
@CrossOrigin(origins = "http://localhost:8081")
public class TransactionController {
    private final TransactionService transactionService;

    public TransactionController(TransactionService transactionService) {
        this.transactionService = transactionService;
    }

    @GetMapping("/{id}")
    public Transaction getTransactionById(@PathVariable int id) {
        System.out.println("Received request for all transactions");
        return transactionService.getTransactionById(id);
    }

    @GetMapping
    public List<Transaction> getAllTransactions() {
        return transactionService.getAllTransactions();
    }

    @PostMapping
    public void createTransaction(@RequestBody Transaction transaction) {
        transactionService.insertTransaction(transaction);
    }

    @PutMapping("/{id}")
    public void updateTransaction(@PathVariable int id, @RequestBody Transaction transaction) {
        transaction.setId(id);
        transactionService.updateTransaction(transaction);
    }

    @DeleteMapping("/{id}")
    public void deleteTransaction(@PathVariable int id) {
        transactionService.deleteTransaction(id);
    }
}
